import axios from 'axios'
import { ElNotification } from 'element-plus'

const request = axios.create({
    baseURL: 'http://localhost:3000/',
    timeout: 5000
})

// 请求拦截器
request.interceptors.request.use(config => {
    return config;
}, error => {
    ElNotification({
        title: '请求错误',
        message: error.message || '请求发送失败',
        type: 'error',
    });
    return Promise.reject(error);
});

// 响应拦截器
request.interceptors.response.use(response => {
    if (response.data && response.data.code === 200) {
        return response.data;
    } else {
        ElNotification({
            title: 'Error',
            message: response.data?.message ,
            type: 'error',
        });
        return Promise.reject(response.data);
    }
}, error => {
    ElNotification({
        title: '错误',
        message: error.response?.data?.message || error.message || '请求失败',
        type: 'error',
    });
    return Promise.reject(error);
});

export default request